import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * Classe permettant de dessiner les cases du damier 
 * @author anane & morchid & laaqoud
 *	Version 1.0
 */

@SuppressWarnings("serial")
public class Cases extends JPanel {
	

	public static BufferedImage imageReine;
	public static BufferedImage imageFond;
	public static BufferedImage imageFond1;
	public static BufferedImage imageFond2;

	private Color couleur;
	private boolean type; //False = vide / True = Occupee
	private int numero_reine;
	private String S_numero_reine;
	

	public Cases(Color c, boolean active, int numero){
		this.couleur = c;
		this.type = active;
		this.numero_reine = numero;
		this.setBackground(couleur);		
		this.S_numero_reine = String.valueOf(numero_reine);

		try{
			if(c.equals(Color.BLACK))
				imageFond1 = ImageIO.read(new File("fond1.jpg"));
			else
				imageFond2 = ImageIO.read(new File("fond2.jpg"));
		}
		catch (IOException e){
			e.printStackTrace();
		}
	}

	
	public void paintComponent(Graphics g){
		super.paintComponent(g);

		if(this.couleur.equals(Color.BLACK))
			imageFond = imageFond1;
		else
			imageFond = imageFond2;
		
		//case occupee
		if(this.getType() == true){ 

			int width = getWidth();
			int height = getHeight();

			int gauche = (int) ((width-width/1.5)/2);
			int droite = (int) ((height-height/1.5)/2);
			int hauteur = (int) (width/1.5);
			int largeur = (int) (height/1.5);

			int taillePolice = (int)(hauteur*0.2);
			int xPolice = (int) ((width-width/1.2)/2);
			int yPolice = (int) ((height-width/2.1)/2);

			try {
				if(this.numero_reine >= 1){ // reine inactive
					imageReine = ImageIO.read(new File("reine.png"));
					g.drawImage(imageFond, 0, 0, width, height, this);
					g.drawImage(imageReine, gauche, droite, hauteur, largeur, this);
					g.setColor(Color.GRAY);
					g.setFont(new Font("sansserif", Font.BOLD, taillePolice));
					g.drawString(S_numero_reine, xPolice, yPolice);
				}
			}
			catch (IOException e){
				e.printStackTrace();
			}
		}
		//case vide
		else if(this.getType() == false)
			g.drawImage(imageFond, 0, 0,getWidth(), getHeight(), this);
	}

	public Color getCouleur(){
		return couleur;
	}

	public void setCouleur(Color couleur){
		this.couleur = couleur;
	}

	public boolean getType(){
		return type;
	}

	public void setType(boolean type) {
		this.type = type;
	}
}
